package com.prdiot.poetry.controller;

import com.prdiot.poetry.domains.Mingju;
import com.prdiot.poetry.domains.Poems;
import com.prdiot.poetry.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class MingjuController {

    @Autowired
    PoemsService poemsService;

    @Autowired
    TagsService tagsService;

    @Autowired
    ChengyuService chengyuService;

    @Autowired
    MingjuService mingjuService;

    @Autowired
    AuthorService authorService;

    @GetMapping("/mingju")
    public String index(Model model,
                        @RequestParam(value="theme",required=false,defaultValue="") String theme,
                        @RequestParam(value="page",required=false,defaultValue="1") String page){

        int curPage = 1;
        try{
            curPage = Math.abs(Integer.valueOf(page));
        }catch (Exception exception){
            curPage = 1;
        }
        int pageCount = 0;

        List<Mingju> mingjuList = new ArrayList<>();
        if (!StringUtils.isEmpty(theme)){
            Sort sort = new Sort(Sort.Direction.ASC, "code");
            Pageable pageable = PageRequest.of(curPage - 1, 10, sort);
            Page<Mingju> mingjuPage = mingjuService.searchByTheme(theme,pageable);
            mingjuList = mingjuPage.getContent();
            pageCount = mingjuPage.getTotalPages();
        }else {
            mingjuList = mingjuService.getRandMingju();
        }


        Sort sort = new Sort(Sort.Direction.ASC, "code");
        Pageable pageable = PageRequest.of(curPage - 1, 10, sort);

        model.addAttribute("page", curPage);
        model.addAttribute("pageCount", pageCount);
        model.addAttribute("themeList",mingjuService.getMingjuTheme());
        model.addAttribute("mingjuList",mingjuList);
        model.addAttribute("theme", theme);
        model.addAttribute("tagList",tagsService.getTagsOrderByPoemCount(100));
        model.addAttribute("chengyuList",chengyuService.getRandPoetry());
        return "mingjulist";
    }

    @GetMapping("/mingju/{id}")
    public String findMingju(Model model,@PathVariable("id") String id){
        Mingju mingju = mingjuService.findByCode(id);

        model.addAttribute("mingju",mingju);
        model.addAttribute("themeList",mingjuService.getMingjuTheme());
        model.addAttribute("mingjuList",mingjuService.getRandMingju());
        model.addAttribute("tagList",tagsService.getTagsOrderByPoemCount(100));
        model.addAttribute("chengyuList",chengyuService.getRandPoetry());
        return "mingju";
    }


}
